package com.hdu;


import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import com.hdu.redis.KeyStringRedisSerializer;
import com.hdu.redis.RedisShadowKeyConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;


@Configuration
@EnableConfigurationProperties(RedisShadowKeyConfiguration.class)
public class FullLinkRedisAutoConfiguration {

    @Bean
    public KeyStringRedisSerializer keyStringRedisSerializer(RedisShadowKeyConfiguration redisShadowKeyConfiguration) {
        return new KeyStringRedisSerializer(redisShadowKeyConfiguration);
    }


    @SuppressWarnings("all")
    @Bean("redisTemplate")
    public RedisTemplate<Object, Object> redisTemplate(
            KeyStringRedisSerializer keyStringRedisSerializer,
            RedisConnectionFactory factory
    ) {
        RedisTemplate redisTemplate = new RedisTemplate<>();

        FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);

        redisTemplate.setValueSerializer(fastJsonRedisSerializer);
        redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
        redisTemplate.setKeySerializer(keyStringRedisSerializer);
        redisTemplate.setHashKeySerializer(keyStringRedisSerializer);
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate(KeyStringRedisSerializer keyStringRedisSerializer,
                                                   RedisConnectionFactory factory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setKeySerializer(keyStringRedisSerializer);
        stringRedisTemplate.setHashKeySerializer(keyStringRedisSerializer);
        stringRedisTemplate.setConnectionFactory(factory);
        return stringRedisTemplate;
    }
}
